package com.springclouddemo.rabbitmq;

import com.springclouddemo.rabbitmq.config.RabbitConfig;
import com.springclouddemo.rabbitmq.config.TestDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SimpleProducer {
    private RabbitTemplate rabbitTemplate;

    @Autowired
    public SimpleProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * 发生消息到RabbitMQ,使用SpringBoot默认的交换机<br>
     *
     * @param message
     */
    public void sendMessage(String queue, String message) {
        rabbitTemplate.convertAndSend(queue, message);
        System.out.println("simple sendMessage:"+message);
    }

    /**
     * 消息体为对象。配置MessageConverter为Jackson2JsonMessageConverter即可
     * @param user
     */
    public void sendObjMessage(TestDto dto){
        rabbitTemplate.convertAndSend(RabbitConfig.OBJECT_QUEUE, dto);
    }
}
